/////////////////////////登录
//微信自动登录绑定 pc端注册 微信端登录 
//结尾没有拼接参数干净的链接 正常登录
//alert(document.referrer);
if(/signout/.test(location.href)){
    $.cookie("MYAVA",null);
    $.cookie("MYID",null);
    $.cookie("MYNICK",null);
    $.cookie("changeQue",null);
    $.cookie("eid",null);
    $.cookie("fromuser",null);
    $.cookie("pexpid",null);
    $.cookie("pexpuid",null);
    $.cookie("pulucurrentIndex",null);
    $.cookie("pulutype",null);
    $.cookie("sharenewsid",null);
    $.cookie("newsid",null);
    $.cookie("wx",null);//微信首页地址中有code进行判断
    $.cookie("shareNewsListScroll",null);
    $.cookie("lastInfoIndex",null);
    $.cookie("lastInfoScroll",null);
    $.cookie("setpageLastNoteType",null);
    $.cookie("selectType",null);//经验类别
    $.cookie("validateLevel","");
}
//扫码加好友
if(/login.html#fromuser=[0-9a-z]+/.test(location.href)){
    fromuser=(location.href).match(/fromuser=[a-z0-9]+/)[0].split("=")[1];
    if(/[0-9]/.test(fromuser)){$.cookie("fromuser",fromuser);}
}
$().ready(function(){
    if(is_weixn()&&/login.html$/.test(location.href))
    {
        if(!$.cookie("wxopenid")){
            location.href="https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx658c34793ecfd9f9&redirect_uri=https://www.pulu.com/weixin/login.html&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";
        }
    }
//结尾拼接的是微信code
    else if(is_weixn()&&/code=/.test(location.href)){
        var reg=new RegExp("code=[a-zA-Z0-9]*");
        var code=location.href.match(reg)[0];
        $.ajax({
            type:"GET",
            url:"GetOpenId.ashx?"+code,
            async: false,
            success:function(data){
                var dat=data;
                //保存到本地后 进行重定向
                $.cookie("wxopenid",dat);
                location.href="https://www.pulu.com/weixin/login.html";
            }
        })
    }
});
//------------------------
var autologintype=true;
if(localStorage.getItem("mp")){
    $("#autoimg").css({"background":"url(img/hook01.png)no-repeat center","background-size":"17px"});
    autologintype=true;
    $("#phonenumber").val(localStorage.getItem("mp"));
    $("#password").val(localStorage.getItem("pw"));
    //自动登录
    //不是退出
    if(!$.cookie("issignout")){
        $.cookie("MYID",localStorage.getItem("MYID"));
        if(/subscribe/.test(location.href)){
            if(is_weixn()){
                tohrefurl="http://www.pulu.com/weixin/pulu.html";
            }else{
                tohrefurl="http://www.pulu.com/weixin/subscribe.html";
            }
        }else{
            location.href="http://www.pulu.com/weixin/pulu.html";
        }
    }else if($.cookie("issignout")&&$.cookie("issignout")=="退出登录"){
        //退出登录
        $("#phonenumber").val("");
        $("#password").val("");
        $.cookie("mp","");
        $.cookie("pw","");
        $.cookie("MYID","");
        localStorage.clear();
    }
}else{
    $("#autoimg").css({"background":"url(img/hook01.png)no-repeat center","background-size":"17px"});
    $("#phonenumber").focus();
}
//选择自动登录
$("#autologin>div").click(function(){
    var aaa=document.getElementById("autoimg").style.background;
    if(/hook01/.test(aaa)){
        autologintype=false;
        $("#autoimg").css({"background":"url(img/hook-no.png)no-repeat center","background-size":"17px"});
        var storage=window.localStorage;
        storage.clear();
    }else if(/hook-no/.test(aaa)){
        autologintype=true;
        $("#autoimg").css({"background":"url(img/hook01.png)no-repeat center","background-size":"17px"});
    }
});
if($.cookie("mp")){
    $("#phonenumber").val($.cookie("mp"));
    $("#password").val($.cookie("pw"));
}
//判断是否在微信打开
function is_weixn(){
    var ua = navigator.userAgent.toLowerCase();
    if(ua.match(/MicroMessenger/i)=="micromessenger") {
        return true;
    } else {
        return false;
    }
}
var tohrefurl="https://www.pulu.com/weixin/pulu.html";//原始跳转页面
if(/subscribe/.test(location.href)){tohrefurl="https://www.pulu.com/weixin/subscribe.html";}
if(is_weixn()){
    tohrefurl="http://www.pulu.com/weixin/pulu.html";
    if(/subscribe/.test(location.href)){tohrefurl="http://www.pulu.com/weixin/subscribe.html";}
}
/*
 * 登录方法
 */
function loginftn(){
    //if(!$("#phonenumber").val().match(/^1[34578]\d{9}$/)){
    if(!$("#phonenumber").val().match(/^[0-9]+$/)){
        if(/^\s+$/.test($("#phonenumber").val())||!$("#phonenumber").val()){
            $("#tishiinfor").html("手机号不能为空");
        }else{
            $("#tishiinfor").html("手机号不正确");
        }
    }else if(!$("#password").val()||/^\s+$/.test($("#password").val())){$("#tishiinfor").html("密码不能为空");
    }else{
        var phonenumber=$("#phonenumber").val();
        var password=$("#password").val();
        var md5phonenumber=hex_md5(phonenumber).substring(7, 23);
        var md5password=hex_md5(password).substring(7, 23);
        $("#tishiinfor").html("正在登录...");
//    环信
        //创建链接
        var conn = new WebIM.connection({
            https: WebIM.config.https,
            url: WebIM.config.xmppURL,
            isAutoLogin: WebIM.config.isAutoLogin,
            isMultiLoginSessions: WebIM.config.isMultiLoginSessions
        });
        //登录
        var options = {
            apiUrl: WebIM.config.apiURL,
            user: md5phonenumber,
            pwd: md5phonenumber,
            appKey: "pulu#pulu"
        };
        conn.open(options);
        conn.listen({
            onOpened: function ( message ) {          //连接成功回调
                //如果isAutoLogin设置为false，那么必须手动设置上线，否则无法收消息
                conn.setPresence();
                console.log("连接成功");
            },
            onRoster: function ( message ) {

            }         //处理好友申请
        });
        var otherUserId="";
        if($.cookie("fromuser")&&/[0-9]/.test(location.href)){otherUserId=$.cookie("fromuser")};
        if(/login.html#fromuser=[0-9a-z]+/.test(location.href)){
            otherUserId=(location.href).match(/fromuser=[a-z0-9]+/)[0].split("=")[1];
            if(/[0-9]/.test(otherUserId)){$.cookie("fromuser",otherUserId);}
        }
        $.ajax({
            type:"post",
            url:"https://www.pulu.com/webapi/api/PuluUser/Login",
            data:{"userId":md5phonenumber,"password":md5password,"otherUserId":otherUserId},
            success:function(d){
                if(d.Message==="密码错误"){
                    $("#tishiinfor").html("密码错误");
                }
                else if(d.Message==="手机号未注册"){
                    $("#tishiinfor").html("手机号未注册");
                }
                else if(d.Message==="正常登录"){
                    $("#tishiinfor").html("");
                    $.cookie("MYNICK", d.Data.userNick);
                    $.cookie("MYID",d.Data.userId);
                    $.cookie("MYAVA", d.Data.userAva);
                    if(d.Data.validateLevel){
                        $.cookie("validateLevel",d.Data.validateLevel);
                    }else{
                        $.cookie("validateLevel",0);
                    }
                    //如果有 fromeuser 转发注册页面跳转进来
                    if($.cookie("fromuser")){
                        //唯一途径从注册页进入 获取注册页传递的cookie数据  (扫码进入)
                        var loca=$.cookie("fromid");
                        if(/newsid=/.test(loca)){
                            var newsid=loca.match(/newsid=[0-9]+/)[0].match(/[0-9]+/)[0];
                            tohrefurl="http://www.pulu.com/weixin/newsdetail.html?newsid="+newsid+"#fromuser="+md5phonenumber;
                        }else if(/qid=/.test(loca)){
                            var qid=loca.match(/qid=[0-9]+/)[0].match(/[0-9]+/)[0];
                            tohrefurl="http://www.pulu.com/weixin/questiondetail.html?qid="+qid+"#fromuser="+md5phonenumber;
                        }else if(/eid=/.test(loca)){
                            var eid=loca.match(/eid=[0-9]+/)[0].match(/[0-9]+/)[0];
                            tohrefurl="http://www.pulu.com/weixin/shareexppage.html?eid="+eid+"#fromuser="+md5phonenumber;
                        }
                    }

                    if($.cookie("wxopenid")){
                        $.ajax({
                            type:"post",
                            url:"https://www.pulu.com/webapi/api/WebChatInfo/AddWebChatInfo",
                            data:{"userId":md5phonenumber,"webchatId":$.cookie("wxopenid")},
                            success:function(d){
                                $.cookie("issignout","");location.href=tohrefurl;
                            }
                        })
                    }
                    else{
                        //如果有 fromeuser 转发注册页面跳转进来  加好友
                        if($.cookie("fromuser")){
                            $.cookie("issignout","");location.href=tohrefurl;
                        }
                        else{
                            if(autologintype){
                                var los=window.localStorage;
                                los.setItem("mp",phonenumber);
                                los.setItem("pw",password);
                                los.setItem("MYID",md5phonenumber);
                            }
                            $.cookie("issignout","");location.href=tohrefurl;
                        }
                    }
                }
                else if(d.Message==="数据处理异常，请稍微再试"){
                    $("#tishiinfor").html("数据处理异常，请稍后再试");
                }
            }
        });
    }
}
$("#denglu>button").click(function(){
    loginftn();
});
document.onkeydown=function(event){
    var e = event || window.event || arguments.callee.caller.arguments[0];
    if(e && e.keyCode==13){ // enter 键
        loginftn();
    }
};
$('input').bind('input propertychange', function() {
    $("#tishiinfor").html("");
});
//忘记密码
$("#forgetpw").click(function(){
    location.href="forgetpw.html";
});